package com.lyubia.shop.manager.controller;

import com.lyubia.shop.common.result.BaseResult;
import com.lyubia.shop.common.result.FileResult;
import com.lyubia.shop.manager.pojo.GoodsImages;
import com.lyubia.shop.manager.service.FileUploadService;
import com.lyubia.shop.manager.service.GoodsImagesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/fileUpload")
public class FileUploadController {

    @Resource
    private FileUploadService fileUploadService;

    @Resource
    private GoodsImagesService goodsImagesService;

    /**
     * 单文件上传
     * @param multipartFile
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public FileResult fileUpload(@RequestParam("file") MultipartFile multipartFile) {
        FileResult fileResult = new FileResult();
        if (multipartFile.isEmpty()) {
            fileResult.setError("文件内容为空!");
            fileResult.setMsg("文件内容为空!");
            return fileResult;
        }
        try {
            String oldName = multipartFile.getOriginalFilename();
            String newName = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
                    + "#" + System.currentTimeMillis() + oldName.substring(oldName.lastIndexOf("."));
            fileResult = fileUploadService.fileUpload(multipartFile.getInputStream(),newName);
        } catch (IOException e) {
            e.printStackTrace();
            fileResult.setError("文件上传失败!");
            fileResult.setMsg("文件上传失败!");
        }
        return fileResult;
    }

    /**
     * 多文件上传
     * @param multipartFile
     * @return
     */
    @PostMapping("/images/save")
    @ResponseBody
    public BaseResult manyFileUpload(@RequestParam("file") MultipartFile multipartFile, Integer goodsId) {
        if (multipartFile.isEmpty()) {
            return BaseResult.error("文件为空!");
        }
        if (goodsId == null) {
            return BaseResult.error("商品id为空");
        }
        try {
            String oldName = multipartFile.getOriginalFilename();
            String newName = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
                    + "#" + System.currentTimeMillis() + oldName.substring(oldName.lastIndexOf("."));
            FileResult fileResult = fileUploadService.fileUpload(multipartFile.getInputStream(),newName);
            if (!StringUtils.isEmpty(fileResult.getFileUrl())) {
                GoodsImages goodsImages = new GoodsImages();
                goodsImages.setImageUrl(fileResult.getFileUrl());
                goodsImages.setGoodsId(goodsId);
                return goodsImagesService.saveGoodsImages(goodsImages);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return BaseResult.error("文件上传失败!");
    }

}
